package com.enoram.gwt.desktop.server;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class DesktopMemRealm extends AuthorizingRealm{

	String userDB = "admin_d74ff0ee8da3b9806b18c877dbf29bbde50b5bd8e4dad7a3a725000feb82e8f1";
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		//get the principal this realm cares about:
        String userName = (String) getAvailablePrincipal(principals);
        
        UserAccount userAccount = bindUserAccount(userName);
		return userAccount;
	}

	protected UserAccount bindUserAccount(String userName){
		
		String[] users = userDB.split("_");
		if(users[0].equals(userName)){
		
			UserAccount userAccount = new UserAccount(userName, users[1], "desktop");
			return userAccount;
		}
		return null;
	}
	
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;

		return bindUserAccount(upToken.getUsername());
	}
	
		

}
